﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebCore_MVC.Models;
using WebCore_MVC.Services;
using Microsoft.EntityFrameworkCore;

namespace WebCore_MVC.Controllers
{
   // [Route("[controller]")]
    public class LoginController : Controller
    {
        protected ServiceContext ServiceContext { get; private set; }

        public LoginController(ServiceContext serviceContext)
        {
            ServiceContext = serviceContext;
        }

        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
       
      
        public IActionResult Login()
        {       
            return View();        
        }


      
      

        [HttpPost]
        public async Task<IActionResult> LoginProcess(User user)
        {
            ViewData["Msg"] = 0;
            if (ModelState.IsValid)
            {


                var entity = await ServiceContext.DbContext.Set<User>().Where(t => t.Usr == user.Usr).FirstOrDefaultAsync();               
                if (entity == null)
                {
                    return Content("<script >alert('该用户名不存在');location.href='/Login/Login'</script >", "text/html");
                }
                else if(entity.Pwd!=user.Pwd)
                {
                    return Content("<script >alert('密码错误');location.href='/Login/Login'</script >", "text/html");
                }

                else return RedirectToAction("Index");

            }
            return View();
        } 
    }
}
